from odoo import fields, models, api


class ProductProduct(models.Model):
    _inherit = 'product.product'

    # 库存数量
    s_actual_quantity = fields.Float(string='Actual qty', compute='_compute_actual_quantity', store=True)

    s_pre_qty = fields.Float('Pre Production Qty', compute='_compute_actual_quantity', store=True)
    # 生产前可用数量
    pre_production_qty = fields.Float(string='Pre Production', compute='_compute_actual_quantity', store=True)

    # 生产前预留的数量
    pre_production_count = fields.Float(string='Pre Production Count', compute='_compute_actual_quantity', store=True)

    s_lock_qty = fields.Float('Lock Qty', compute='_compute_lock_qty')  # 物料锁定数量
    s_lock_stock_qty = fields.Float('Lock Stock Qty', compute='_compute_lock_qty')  # 物料库存锁定数量
    s_lock_prepare_qty = fields.Float('Lock Prepare Qty', compute='_compute_lock_qty')  # 物料库存锁定数量
    s_lock_ids = fields.One2many('s.product.lock', 'product_id')

    s_free_qty = fields.Float('free qty', compute='_compute_lock_qty')

    @api.depends('qty_available')
    def _compute_actual_quantity(self):
        for product in self:
            # 生产前位置
            before_production_location = self.env['stock.location'].search([('barcode', '=', 'WH-PREPRODUCTION')])
            before_production_location |= before_production_location.mapped('child_internal_location_ids')

            # 库存位置
            stock_location = self.env['stock.location'].search([('barcode', '=', 'WH-STOCK')])
            stock_location |= stock_location.mapped('child_internal_location_ids')
            before_production_location_quants = self.env['stock.quant'].search([
                ('product_id', '=', product.id),
                ('location_id.usage', '=', 'internal'),
                ('location_id', 'in', before_production_location.ids),
                ('quantity', '>', 0)
            ])
            stock_location_quants = self.env['stock.quant'].search([
                ('product_id', '=', product.id),
                ('location_id.usage', '=', 'internal'),
                ('location_id', 'in', stock_location.ids),
                ('quantity', '>', 0)
            ])

            pre_production_qty = sum(before_production_location_quants.mapped('quantity'))
            pre_production_count = sum(before_production_location_quants.mapped('reserved_quantity'))
            product.s_pre_qty = pre_production_qty
            product.s_actual_quantity = sum(stock_location_quants.mapped('quantity'))
            product.pre_production_qty = max(pre_production_qty - min(pre_production_qty, pre_production_count), 0)
            product.pre_production_count = min(pre_production_qty, pre_production_count)

    @api.depends('s_lock_ids', 's_lock_ids.qty', 's_lock_ids.uom_id')
    def _compute_lock_qty(self):
        for rec in self:
            lock_ids = rec.s_lock_ids.filtered(lambda x: not x.cancel and not x.x_delete)
            rec.s_lock_qty = sum([line.uom_id._compute_quantity(line.qty, rec.uom_id) for line in lock_ids])
            rec.s_lock_stock_qty = sum([line.uom_id._compute_quantity(line.qty, rec.uom_id) for line in lock_ids.filtered(lambda x: x.lock_type == 'stock')])
            rec.s_lock_prepare_qty = sum([line.uom_id._compute_quantity(line.qty, rec.uom_id) for line in lock_ids.filtered(lambda x: x.lock_type == 'prepare')])
            rec.s_free_qty = rec.qty_available - rec.outgoing_qty - rec.s_lock_stock_qty

    def action_view_lock_line(self):
        self.ensure_one()
        action = self.env["ir.actions.act_window"]._for_xml_id('sb_base.action_s_product_lock')
        action['domain'] = [('product_id', '=', self.id)]
        return action

    def compute_version_qty_available(self):
        """
        查询多版本在手库存
        """
        self.ensure_one()
        product_ids = self.s_base_id.product_tmpl_ids.mapped('product_variant_ids')
        product_ids |= self

        # 库存在手
        return sum(product_ids.mapped('s_actual_quantity'))

    def _check_product_is_cr(self):
        """ 判断物料是否为芯板 """
        self.ensure_one()
        if self.categ_id.parent_id and self.categ_id.parent_id.code and self.categ_id.parent_id.code == 'CR':
            return True
        return False
